#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from Config.PackPath import CONFIG_EXCEL_DIR, CONFIG_EXCEL_NAME_LIST, CONFIG_DIR
from Utils.Logger import logger
from Src.ConfigExcelParser import ConfigExcelParser
import json
import os


def load_config_excels_obj():
    config_excel_obj_dict = dict()
    for excel_name in CONFIG_EXCEL_NAME_LIST:
        excel_path = os.path.join(CONFIG_EXCEL_DIR, excel_name)
        if not os.path.exists(excel_path):
            logger.warning('工作簿[{0}]不存在，请检查文件'.format(excel_path))
        else:
            excel_obj = ConfigExcelParser(excel_path)
            config_excel_obj_dict[excel_name] = excel_obj
    return config_excel_obj_dict


def load_config_excels_xpath():
    obj_dict = load_config_excels_obj()
    xpath_file_path = os.path.join(CONFIG_DIR, 'ConfigExcelXpath.json')
    xpath_group = dict()
    for excel_name, excel_obj in obj_dict.items():
        xpath_group[excel_name] = excel_obj.get_xpath_list()
    try:
        with open(xpath_file_path, 'w', encoding='utf-8') as w:
            json.dump(xpath_group, w, ensure_ascii=False, indent=4)
    except Exception as e:
        logger.error('保存ConfigExcelXpath.json文件失败:[{0}]'.format(e[:-1]))
    return xpath_group


def load_script_excels_config():
    config_file_path = os.path.join(CONFIG_DIR, 'ScriptExcelConfig.json')
    config_dict = dict()
    try:
        with open(config_file_path, 'r', encoding='utf-8') as r:
            config_dict = json.load(r)
    except Exception as e:
        logger.error('读取ScriptExcelConfig.json文件失败:[{0}]'.format(e[:-1]))
    return config_dict


def load_local_machine_config():
    config_file_path = os.path.join(CONFIG_DIR, 'LocalMachineConfig.json')
    config_dict = dict()
    try:
        with open(config_file_path, 'r', encoding='utf-8') as r:
            config_dict = json.load(r)
    except Exception as e:
        logger.error('读取ScriptExcelConfig.json文件失败:[{0}]'.format(e[:-1]))
    return config_dict


if __name__ == '__main__':
    a = load_local_machine_config()
    for k, v in a.items():
        print(k, v)
